<?php
    /**
     * User: Lee
     * Date: 2017/9/29
     */

    namespace Home\Service;

    use Home\DAO\WSBillDAO;
    use Home\DAO\SOBillDAO;
    use Home\DAO\ITBillDAO;

    class BillApiService extends PSIBaseExService{
        private $LOG_SO = "销售订单";
        private $LOG_WX = "销售出库";
        private $LOG_IT = "库间调拨";

        /**
         * 新增销售订单接口
         */
        public function addSOBill_api( $json ){
            if( !is_null( json_decode( $json ) ) ){
                $bill = json_decode( $json );
            } else{
                $bill = $json;
            }
            if( $bill == null ){
//			return $this->bad( "传入的参数错误，不是正确的JSON格式" );
                return $this->bad( "传入的参数不能为空" );
            }

            $db = $this->db();

            $db->startTrans();

            $SODao = new SOBillDAO( $db );

            $log = null;

            // 新建销售订单
            $bill[ "loginUserId" ] = $this->getLoginUserId();
            $bill[ "dataOrg" ] = $this->getLoginUserDataOrg();
            $bill[ "companyId" ] = $this->getCompanyId();

            $rc = $SODao->addSOBill( $bill );
            if( $rc ){
                $db->rollback();
                return $rc;
            }

            $SOid = $bill[ "id" ];
            $SOref = $bill[ "ref" ];

            $log = "新建销售订单，单号：{$SOref}";


            // 记录业务日志
            $bs = new BizlogService( $db );
            $bs->insertBizlog( $log, $this->LOG_SO );

            //审核
            $params = array();
            $params['id'] = $SOid;
            $params['loginUserId'] = $this->getLoginUserId();
            $rc = $SODao->commitSOBill($params);
            if ($rc) {
                $db->rollback();
                return $rc;
            }

            // 记录业务日志
            $SOref = $params["ref"];
            $log = "审核销售订单，单号：{$SOref}";
            $bs->insertBizlog($log, $this->LOG_SO);

            // 从销售订单生成销售出库单
            if(!isset($bill['bizDT'])){
                //如果没有设定业务日期，则业务日期为交货日期
                $bill['bizDT'] = $bill['dealDate'];
            }
            $dao = new WSBillDAO( $db );

            $params = array();
            $params['sobillRef'] = $bill['sobillRef'];
            $params["loginUserId"] = $this->getLoginUserId();
            $params["loginUserName"] = $this->getLoginUserName();
            $params["companyId"] = $this->getCompanyId();

            //获取销售订单详细信息
            $billInfo = $dao->wsBillInfo($params);
            foreach($billInfo['items'] as $k => $v){
                //将销售单中产品详细id 传到 bill数组的 items中每个产品单 下
                $bill['items'][$k]['soBillDetailId'] = $v['id'];
            }

            $rc = $dao->addWSBill( $bill );
            if( $rc ){
                $db->rollback();
                return $rc;
            }

            $WSid = $bill['id'];
            $WSref = $bill[ 'ref' ];  //出库单单号
            $sobillRef = $bill['sobillRef'];    //销售单单号


            $log = "从销售订单(单号：{$sobillRef})生成销售出库单: 单号 = {$WSref}";

            // 记录业务日志
            $bs->insertBizlog( $log, $this->LOG_WX );

            //$params数组添加值
            $params['id'] = $WSid;

            //提交出库单
            $rc = $dao->commitWSBill($params);
            if ($rc) {
                $db->rollback();
                return $rc;
            }

            $log = "提交销售出库单，单号 = {$WSref}";
            $bs->insertBizlog($log, $this->LOG_WX);

            $db->commit();

            return $this->ok( $SOid );
        }

        /**
         * 新增销售出库单接口
         */
        public function addWSBill_api( $params ){
            if( !is_null( json_decode( $params ) ) ){
                $bill = json_decode( $params );
            } else{
                $bill = $params;
            }
            if( $bill == null ){
//			return $this->bad( "传入的参数错误，不是正确的JSON格式" );
                return $this->bad( "传入的参数不能为空" );
            }

            $db = $this->db();
            $db->startTrans();

            $dao = new WSBillDAO( $db );

            $log = null;

            // 新建销售出库单

            $bill[ "dataOrg" ] = $this->getLoginUserDataOrg();
            $bill[ "companyId" ] = $this->getCompanyId();
            $bill[ "loginUserId" ] = $this->getLoginUserId();

            $rc = $dao->addWSBill( $bill );
            if( $rc ){
                $db->rollback();
                return $rc;
            }

            $id = $bill[ "id" ];
            $ref = $bill[ "ref" ];
            // 手工新建销售出库单
            $log = "新增销售出库单，单号 = {$ref}";

            // 记录业务日志
            $bs = new BizlogService( $db );
            $bs->insertBizlog( $log, $this->LOG_WX );

            $params = array();
            $params['id'] = $id;
            $params["companyId"] = $this->getCompanyId();
            $params["loginUserId"] = $this->getLoginUserId();


            // 记录业务日志
            $bs->insertBizlog( $log, $this->LOG_WX );

            //提交出库单
            $rc = $dao->commitWSBill($params);
            if ($rc) {
                $db->rollback();
                return $rc;
            }

            $log = "提交销售出库单，单号 = {$ref}";
            $bs->insertBizlog($log, $this->LOG_WX);

            $db->commit();

            return $this->ok( $id );
        }

        /**
         * 新建调拨单
         */
        public function addITBill_api( $params ){
            if( !is_null( json_decode( $params ) ) ){
                $bill = json_decode( $params,true );
            } else{
                $bill = $params;
            }
            if( $bill == null ){
//			return $this->bad( "传入的参数错误，不是正确的JSON格式" );
                return $this->bad( "传入的参数不能为空" );
            }

            $db = $this->db();
            $db->startTrans();

            $dao = new ITBillDAO( $db );

            $log = null;

            $bill[ "loginUserId" ] = $this->getLoginUserId();
            $bill[ "dataOrg" ] = $this->getLoginUserDataOrg();
            $bill[ "companyId" ] = $this->getCompanyId();

            $rc = $dao->addITBill( $bill );
            if( $rc ){
                $db->rollback();
                return $rc;
            }

            $id = $bill[ "id" ];
            $ref = $bill[ "ref" ];
            $log = "新建调拨单，单号：$ref";

            $bs = new BizlogService( $db );
            $bs->insertBizlog( $log, $this->LOG_IT );

            //提交调拨单
            $params = array();
            $params['id'] = $id;
            $rc = $dao->commitITBill($params);
            if ($rc) {
                $db->rollback();
                return $rc;
            }

            $ref = $params["ref"];

            // 记录业务日志
            $log = "提交调拨单，单号: $ref";
            $bs->insertBizlog($log, $this->LOG_IT);

            $db->commit();

            return $this->ok( $id );
        }
    }